package com.cby.springboot.demo.controller;

import com.cby.springboot.demo.bean.*;
import com.cby.springboot.demo.service.*;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/api")
public class MenuController extends BaseController{

    @Autowired
    UserService userService;

    @Autowired
    AdminUserRoleService adminUserRoleService;

    @Autowired
    AdminRoleMenuService adminRoleMenuService;

    @Autowired
    AdminMenuService adminMenuService;

    @CrossOrigin
    @GetMapping("/menu")
    @ResponseBody
    public List<AdminMenu> menu() {
        return getMenusByCurrentUser();
    }

    @CrossOrigin
    @GetMapping("/admin/role/menu")
    @ResponseBody
    public List<AdminMenu> listAllMenus() {
        return adminMenuService.getMenusByRoleId(1);
    }

    public List<AdminMenu> getMenusByCurrentUser() {
        // 从数据库中获取当前用户  shiro
        //String username = SecurityUtils.getSubject().getPrincipal().toString();
        String username = "jason";
        logI("username = "+username);
        User user = userService.getByUserName(username);

        // 获得当前用户对应的所有角色的 id 列表
        List<Integer> rids = adminUserRoleService.findAllByUid(user.getUserId())
                .stream().map(AdminUserRole::getRid).collect(Collectors.toList());

        // 查询出这些角色对应的所有菜单项
        List<Integer> menuIds = adminRoleMenuService.findAllByRid(rids)
                .stream().map(AdminRoleMenu::getMid).collect(Collectors.toList());
        List<AdminMenu> menus = findAllMenuByIds(menuIds).stream().distinct().collect(Collectors.toList());

        // 处理菜单项的结构
        handleMenus(menus);
        logI("menus = "+menus.size());
        return menus;
    }

    /**
     * 通过menu的id检索所有的菜单
     * @param menuIds
     * @return
     */
    private List<AdminMenu> findAllMenuByIds(List<Integer> menuIds) {
        int size = menuIds == null ? 0 : menuIds.size();
        List<AdminMenu> result = new ArrayList<>();
        if(size > 0) {
            for (int i = 0; i < size; i++) {
                result.add(adminMenuService.findById(menuIds.get(i)));
            }
        }
        return result;
    }

    public void handleMenus(List<AdminMenu> menus) {
        for (AdminMenu menu : menus) {
            List<AdminMenu> children = adminMenuService.findAllByParentId(menu.getId());
            menu.setChildren(children);
        }

        Iterator<AdminMenu> iterator = menus.iterator();
        while (iterator.hasNext()) {
            AdminMenu menu = iterator.next();
            if (menu.getParentId() != 0) {
                iterator.remove();
            }
        }
    }

}
